package rlpw.swing;

import javax.swing.*;

public class JGroupPanel extends JPanel {

    private JComponent[][] compos;
    private GroupLayout.Alignment[] colaligns;
    private boolean[] colresize;
    private GroupLayout layout;
    private int height, width;

    public JGroupPanel(int columns, int rows) {
        this(columns, rows, true, false);
    }

    public JGroupPanel(int columns, int rows, boolean innerGap, boolean outerGap) {
        
        height = rows;
        width = columns;
        compos = new JComponent[width][height];
        colaligns = new GroupLayout.Alignment[width];
        colresize = new boolean[width];
        for (int i = 0; i < width; i++) {
            colaligns[i] = GroupLayout.Alignment.LEADING;
            colresize[i] = true;
        }
        layout = new GroupLayout(this);
        setLayout(layout);
        layout.setAutoCreateGaps(innerGap);
        layout.setAutoCreateContainerGaps(outerGap);

    }

    public void setInnerGap(boolean show) {
        layout.setAutoCreateGaps(show);
    }

    public void setOuterGap(boolean show) {
        layout.setAutoCreateContainerGaps(show);
    }

    public boolean isInnerGap() {
        return layout.getAutoCreateGaps();
    }

    public boolean isOuterGap() {
        return layout.getAutoCreateContainerGaps();
    }

    public void setColumnOptions(int index, GroupLayout.Alignment align, boolean resize) {
        colresize[index] = resize;
        colaligns[index] = align;
        rebuild();
    }

    public void setColumnAlignment(int index, GroupLayout.Alignment align) {
        colaligns[index] = align;
        rebuild();
    }

    public void setColumnResizable(int index, boolean resize) {
        colresize[index] = resize;
        rebuild();
    }

    public boolean isColumnResizable(int index) {
        return colresize[index];
    }

    public GroupLayout.Alignment getColumnAlignment(int index) {
        return colaligns[index];
    }

    public void setComponent(int x, int y, JComponent comp) {
        compos[x][y] = comp;
        rebuild();
    }

    private void rebuild() {
        GroupLayout.SequentialGroup hgroup = layout.createSequentialGroup();
        for (int i = 0; i < width; i++) {
            GroupLayout.ParallelGroup group = layout.createParallelGroup(colaligns[i], colresize[i]);
            for (int j = 0; j < height; j++) {
                if (compos[i][j] != null) {
                    group.addComponent(compos[i][j]);
                }
            }
            hgroup.addGroup(group);
        }

        GroupLayout.SequentialGroup vgroup = layout.createSequentialGroup();
        for (int j = 0; j < height; j++) {
            GroupLayout.ParallelGroup group = layout.createParallelGroup(GroupLayout.Alignment.BASELINE);
            for (int i = 0; i < width; i++) {
                if (compos[i][j] != null) {
                    group.addComponent(compos[i][j]);
                }
            }
            vgroup.addGroup(group);
        }

        layout.setHorizontalGroup(hgroup);
        layout.setVerticalGroup(vgroup);
    }
}
